/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GUIIncluirTeste.java
 *
 * Created on 15/05/2011, 15:30:12
 */

package gui;

import dao.DAOCliente;
import javax.swing.JOptionPane;
import model.Cliente;

/**
 *
 * @author Proprietário
 */
public class GUIIncluirTeste extends javax.swing.JFrame {

    Cliente clienteOrigem = new Cliente();
    DAOCliente daoCliente = new DAOCliente();

    /** Creates new form GUIIncluirTeste */
    public GUIIncluirTeste() {
        initComponents();

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jTextField4 = new javax.swing.JTextField();
        jbSalvarCliente = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jTextField1.setEditable(false);
        jTextField1.setText("jTextField1");

        jTextField2.setText("jTextField2");

        jTextField3.setText("jTextField3");

        jTextField4.setText("jTextField4");

        jbSalvarCliente.setText("Salvar");
        jbSalvarCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbSalvarClienteActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(151, 151, 151)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 239, Short.MAX_VALUE)
                            .addComponent(jTextField3, javax.swing.GroupLayout.DEFAULT_SIZE, 239, Short.MAX_VALUE)
                            .addComponent(jTextField4, javax.swing.GroupLayout.DEFAULT_SIZE, 239, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(327, Short.MAX_VALUE)
                        .addComponent(jbSalvarCliente)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jbSalvarCliente)
                .addContainerGap(150, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbSalvarClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbSalvarClienteActionPerformed
        
        try{
            clienteOrigem.setCli_Nome(jTextField2.getText());
            clienteOrigem.setCli_Endereco(jTextField3.getText());
            clienteOrigem.setCli_CNPJCPF(jTextField4.getText());

            if  (daoCliente.salvar(clienteOrigem) == true) {
                JOptionPane.showMessageDialog(null, "Cliente salvo com sucesso!");
                this.dispose();
            } else {
              JOptionPane.showMessageDialog(null, "Erro ao salvar o cliente.");
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Erro: "+e.getMessage());
        }

    }//GEN-LAST:event_jbSalvarClienteActionPerformed

    public void setCodigoSelecionado(int pCodigoCliente){
        try{
            System.out.println("setCodigoSelecionado: Codigo="+Integer.toString(pCodigoCliente));
            clienteOrigem = daoCliente.getClientePorCodigo(pCodigoCliente);
        } catch (Exception e) {
            System.out.println("erro: setCodigoSelecionado: "+e.getMessage());
        }

        jTextField1.setText(Integer.toString(clienteOrigem.getCli_Codigo()));
        jTextField2.setText(clienteOrigem.getCli_Nome());
        jTextField3.setText(clienteOrigem.getCli_Endereco());
        jTextField4.setText(clienteOrigem.getCli_CNPJCPF());
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GUIIncluirTeste().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JButton jbSalvarCliente;
    // End of variables declaration//GEN-END:variables

}
